package com.huangmb.anymark.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import com.huangmb.anymark.app.R;
import com.huangmb.anymark.app.activity.base.AppBaseActivity;
import com.huangmb.anymark.app.app.AnyMarkApplication;
import com.huangmb.anymark.app.app.AppConstants;
import com.huangmb.anymark.app.fragment.AppBaseFragment;
import com.huangmb.anymark.app.fragment.add.AddLiveFragment;
import com.huangmb.anymark.app.fragment.add.AddPlaceFragment;
import com.huangmb.anymark.app.fragment.add.AddScheduleFragment;
import com.huangmb.anymark.app.fragment.marker.FoodShopFragment;
import com.huangmb.anymark.app.fragment.user.LoginFragment;

import java.util.HashMap;
import java.util.Map;

/**
 * 添加标记页面
 *
 * @author hmb
 * @version 1.0
 * @since 2016/4/26 21:27
 */
public class AddMarkerActivity extends AppBaseActivity {
    private static Map<String, Class<? extends AppBaseFragment>> fragmentMap = new HashMap<>();

    static {
        fragmentMap.put(AppConstants.TYPE_ADD_LIVE, AddLiveFragment.class);
        fragmentMap.put(AppConstants.TYPE_ADD_SCHEDULE, AddScheduleFragment.class);
        fragmentMap.put(AppConstants.TYPE_ADD_PLACE, AddPlaceFragment.class);
    }

    private Toolbar mToolbar;

    public static final void newInstance(Context context, String type) {
        Intent intent = new Intent(context, AddMarkerActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected void onInitView(Bundle savedInstanceState) {
        super.onInitView(savedInstanceState);
        setContentView(R.layout.activity_add_marker);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void afterInitView(Bundle savedInstanceState) {
        super.afterInitView(savedInstanceState);
        String type = getIntent().getStringExtra("type");
        //Class<? extends AppBaseFragment> fragment = fragmentMap.get(type);
        if (!hasLogin()) {
            toast("请登录");
            Intent intent = new Intent(this, LoginActivity.class);
            startActivityForResult(intent, 0);
            return;
        }
        gotoMarkerDetailPage();
        /*if (fragment != null) {
            try {
                AppBaseFragment instance = fragment.newInstance();
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.container, instance)
                        .commit();
                return;
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }*/
        //如果没有找到对应的Fragment则退出
        //finish();
    }

    public void gotoMarkerDetailPage() {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, new AddPlaceFragment())
                .commit();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0 ) {
            if (resultCode == RESULT_OK) {
                gotoMarkerDetailPage();
            } else {
                toast("取消登录");
                finish();
            }
        }
    }
}
